
async function main() {
  try {
    // 初始化连接池
    await db.init();
    
    // 1. 查询示例
    const users = await db.table('users')
      .select(['id', 'name', 'email'])
      .where({ status: 'active' })
      .orderBy('created_at', 'DESC')
      .limit(10)
      .get();
    console.log('活跃用户:', users);
    
    // 2. 获取单条记录
    const user = await db.table('users')
      .where({ id: 1001 })
      .first();
    console.log('用户详情:', user);
    
    // 3. 计数查询
    const total = await db.table('users')
      .where({ status: 'active' })
      .count();
    console.log('活跃用户总数:', total);
    
    // 4. 插入数据
    const newUserId = await db.table('users')
      .insert({
        name: '李四',
        email: 'lisi@example.com',
        status: 'active',
        created_at: new Date()
      });
    console.log('新用户ID:', newUserId);
    
    // 5. 更新数据
    const updated = await db.table('users')
      .where({ id: newUserId })
      .update({
        email: 'new-email@example.com',
        updated_at: new Date()
      });
    console.log('更新记录数:', updated);
    
    // 6. 删除数据
    const deleted = await db.table('users')
      .where({ id: newUserId })
      .delete();
    console.log('删除记录数:', deleted);
    
    // 7. 原始SQL查询
    const result = await db.execute(
      `SELECT * FROM products WHERE price > :price AND category = :category`,
      { price: 100, category: 'electronics' }
    );
    console.log('高价电子产品:', result.rows);
    
    // 8. 事务示例
    await db.executeTransaction(async (conn) => {
      // 在事务中执行多个操作
      await conn.execute(
        `UPDATE accounts SET balance = balance - :amount WHERE id = :id`,
        { amount: 100, id: 'ACC001' }
      );
      
      await conn.execute(
        `UPDATE accounts SET balance = balance + :amount WHERE id = :id`,
        { amount: 100, id: 'ACC002' }
      );
      
      console.log('转账事务执行成功');
    });
    
  } catch (err) {
    console.error('数据库操作出错:', err);
  } finally {
    // 关闭连接池
    await db.close();
  }
}



